package com.zmn.plat.dubbo.impl.orderconfig.comment;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanCopier;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.orderconfig.comment.OrderCommentDIO;
import com.zmn.plat.common.dto.orderconfig.comment.OrderCommentDRO;
import com.zmn.plat.dubbo.interfaces.orderconfig.comment.OrderCommentListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.orderconfig.comment.OrderComment;
import com.zmn.plat.model.entity.orderconfig.comment.OrderCommentQuery;
import com.zmn.plat.services.interfaces.orderconfig.comment.OrderCommentService;
import com.zmn.plat.utils.bean.BeanCopyUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author tanbiao
 * @description：评价内容
 * @since 2019/10/25 9:38
 * @modifier
 **/
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
public class OrderCommentListRemoteServiceImpl implements OrderCommentListRemoteService {
    private Logger logger = LoggerFactory.getLogger(OrderCommentListRemoteService.class);
    private static final String TAG = "订单评论 dubbo 接口";
    @Resource
    private OrderCommentService orderCommentService;

    /**
     * 描述: 根据主键查询评价内容
     *
     * @param id
     * @return OrderCommentDRO
     * @auther: tanbiao
     * @since: 2019/10/23 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<OrderCommentDRO> getById(Integer id) {
        log.info("getById：id:{},", id);
        OrderComment comment = orderCommentService.findByKey(id);
        if (comment == null) {
            logger.error(String.format("[%s]参数:[%s]对应的对象不存在", TAG, id));
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }
        OrderCommentDRO dro = new OrderCommentDRO();
        BeanCopyUtils.copyProperties(comment, dro);
        dro.setBizTypeName(com.zmn.plat.common.dictionary.ProductDict.translateBizType(dro.getBizType(), ","));
        dro.setTypeName(ProductDict.getOrderCommentTypeStr(dro.getType()));
        return ResponseDTO.success(dro);
    }

    /**
     * 描述: 根据条件查询评价内容
     *
     * @param orderCommentDIO
     * @return OrderCommentDRO
     * @auther: tanbiao
     * @since: 2019/10/23 11:11
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<OrderCommentDRO>> listByQuery(OrderCommentDIO orderCommentDIO) {
        log.info("listByQuery：orderCommentDIO:{},", orderCommentDIO);

        OrderCommentQuery query = new OrderCommentQuery();
        BeanCopyUtils.copyProperties(orderCommentDIO, query);
        List<OrderComment> reasons = Optional.ofNullable(orderCommentService.listByQuery(query)).orElse(Collections.emptyList());
        BeanCopier copier = BeanCopier.create(OrderComment.class, OrderCommentDRO.class, false);
        List<OrderCommentDRO> result = reasons.stream().map(comment -> {
            OrderCommentDRO dro = new OrderCommentDRO();
            copier.copy(comment, dro, null);
            dro.setBizTypeName(com.zmn.plat.common.dictionary.ProductDict.translateBizType(dro.getBizType(), ","));

            dro.setTypeName(ProductDict.getOrderCommentTypeStr(dro.getType()));
            return dro;
        }).collect(Collectors.toList());

        return ResponseDTO.success(result);
    }
}
